﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Carozos.BE;

namespace Carozos.DALC
{
    public class InsumoDALC :SqlDataAccess
    {
        public bool insertaInsumo(InsumoBE pInsumo)
        {
            bool result = false;
            SqlCommand cmd = base.createCommand("INSERT INTO Insumo(nombre_insumo,comentario_insumo,marca_insumo, cantidad_insumo) VALUES(@nombre,@comentario,@marca,@cantidad)");
            base.addInParameter(cmd,"@nombre", pInsumo.nombre);
            base.addInParameter(cmd,"@comentario", pInsumo.comentario );
            base.addInParameter(cmd, "@marca", pInsumo.marca);
            base.addInParameter(cmd, "@cantidad", 0);
            base.executeNonQuery(cmd);
            result = true;
            return result;
        }

        public bool actualizaInsumo(InsumoBE pInsumo)
        {
            bool result = false;
            SqlCommand cmd = base.createCommand("UPDATE insumo SET nombre_insumo=@nombre, comentario_insumo=@comentario, marca_insumo=@marca WHERE id_insumo=@codigo");
            base.addInParameter(cmd, "@codigo", pInsumo.codigo);
            base.addInParameter(cmd, "@nombre", pInsumo.nombre);
            base.addInParameter(cmd, "@comentario", pInsumo.comentario );
            base.addInParameter(cmd, "@marca", pInsumo.marca);
            base.executeNonQuery(cmd);
            result = true;
            return result;
        }

        public bool eliminaInsumo(int codigo)
        {
            bool result = false;
            SqlCommand cmd = base.createCommand("DELETE Insumo WHERE id_insumo=@codigo");
            base.addInParameter(cmd, "@codigo", codigo);
            base.executeNonQuery(cmd);
            result = true;
            return result;
        }

        public List<InsumoBE> getInsumosByNombre(string pNombre)
        {
            List<InsumoBE> lista = new List<InsumoBE>();
            SqlCommand cmd = base.createCommandSP("GET_INSUMOS_BY_NOMBRE");
            base.addInParameter(cmd,"@pa", pNombre);
            using(SqlDataReader r = base.executeReader(cmd))
            {
                while (r.Read())
                    lista.Add(createObject(r));   
            }
            return lista;
        }


        private InsumoBE createObject(SqlDataReader reader)
        {
            InsumoBE inb = new InsumoBE(reader.GetInt32(0), reader.GetString(1), reader.GetString(2),
                                               reader.GetInt32(3), reader.GetString(4));
            return inb;
        }

        public bool actualizarStockInsumo(int codigo, int cantidad)
        {
            bool result = false;
            SqlCommand cmd = base.createCommand("UPDATE insumo SET cantidad_insumo=(cantidad_insumo + @cantidad) WHERE id_insumo=@codigo");
            base.addInParameter(cmd,"@codigo", codigo);
            base.addInParameter(cmd,"@cantidad",cantidad);
            base.executeNonQuery(cmd);
            result = true;
            return result;
        }
    }
}
